/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DisciplinaQuestoesGUI.java
 *
 * Created on 18/06/2011, 19:29:35
 */
package GUI;

import bo.DisciplinaBO;
import entidades.Disciplina;
import entidades.GrauDificuldade;
import entidades.Questao;
import entidades.TipoQuestao;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import logic.FuncoesDisciplinaGUI;
import logic.FuncoesQuestoesGUI;

/**
 *
 * @author Ricardo
 */
public class QuestaoEdicaoGUI extends javax.swing.JFrame {
       
    int alteracao = 0;
    int id;
    String nomeDisciplinaAlterar;

    /** Creates new form DisciplinaQuestoesGUI */
    
    public QuestaoEdicaoGUI(Disciplina disciplina, int index , int alterar) {
        if(alterar == 1){
            id = index;
            initComponents();
            List<Questao> listaQuestoes = new ArrayList();
            Questao questao;
            
            try {
                listaQuestoes = disciplina.listarQuestao();
            } catch (Exception ex) {
                Logger.getLogger(QuestaoEdicaoGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            questao = listaQuestoes.get(index);

            nomeDisciplinaAlterar = disciplina.getNomeDisciplina();
            jComboBoxListaDeDisciplinas1.setVisible(false);
                        
            CarregaTopicoDisciplina(disciplina.getNomeDisciplina());
            
            //jListTopicos.setSelectedIndices(FuncoesQuestoesGUI.listarTopicos(disciplina.getTopico()));
            
            jTextAreaEnunciado.setText(questao.getEnunciadoQuestao());
            if(TipoQuestao.DISCURSIVA == questao.getTipoQuestao()){
                jTextFieldOpcaoMEa.setVisible(false);
                jTextFieldOpcaoMEb.setVisible(false);
                jTextFieldOpcaoMEc.setVisible(false);
                jTextFieldOpcaoMEd.setVisible(false);
                jTextFieldOpcaoMEe.setVisible(false);
                jRadioButtonGabaritoMEa.setVisible(false);
                jRadioButtonGabaritoMEb.setVisible(false);
                jRadioButtonGabaritoMEc.setVisible(false);
                jRadioButtonGabaritoMEd.setVisible(false);
                jRadioButtonGabaritoMEe.setVisible(false);
                jRadioButtonDiscursiva.setSelected(true);
                jTextAreaGabaritoDiscursiva.setText(questao.getResposta());            
            }        
            else{
                jTextAreaGabaritoDiscursiva.setVisible(false);
                jRadioButtonMultEscolha.setSelected(true);
                List<String> listaOpcoes = new ArrayList<String>();
                listaOpcoes = questao.getOpcoes();
                jTextFieldOpcaoMEa.setText(listaOpcoes.get(0));
                jTextFieldOpcaoMEb.setText(listaOpcoes.get(1));
                jTextFieldOpcaoMEc.setText(listaOpcoes.get(2));
                jTextFieldOpcaoMEd.setText(listaOpcoes.get(3));
                jTextFieldOpcaoMEe.setText(listaOpcoes.get(4));
                
                switch (questao.getOpcaoCorreta()){
                    
                    case 0:
                    jRadioButtonGabaritoMEa.setSelected(true);
                    case 1:
                    jRadioButtonGabaritoMEb.setSelected(true);
                    case 2:
                    jRadioButtonGabaritoMEc.setSelected(true);
                    case 3:
                    jRadioButtonGabaritoMEd.setSelected(true);
                    case 4:
                    jRadioButtonGabaritoMEe.setSelected(true);
                    default:
                    System.out.println("Nenhuma opção foi selecionada como correta!");
                }      
            }
            if(GrauDificuldade.DIFICIL == questao.getDificuldade())
                jComboBoxNivelDificuldade.setSelectedIndex(2);
            else if(GrauDificuldade.MEDIA == questao.getDificuldade())
                jComboBoxNivelDificuldade.setSelectedIndex(1);
            else
                jComboBoxNivelDificuldade.setSelectedIndex(0);
            
            alteracao = 1;
        }
        else{
            initComponents();
            ExibirDisciplina ();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupTipoQuestao = new javax.swing.ButtonGroup();
        jComboBox1 = new javax.swing.JComboBox();
        buttonGroupGabaritoME = new javax.swing.ButtonGroup();
        jLabelDisciplina = new javax.swing.JLabel();
        jComboBoxListaDeDisciplinas1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListTopicos = new javax.swing.JList();
        jLabelTopicos = new javax.swing.JLabel();
        jLabelTitulo = new javax.swing.JLabel();
        jLabelTipoQuestao = new javax.swing.JLabel();
        jRadioButtonDiscursiva = new javax.swing.JRadioButton();
        jRadioButtonMultEscolha = new javax.swing.JRadioButton();
        jLabelNivelDificuldade = new javax.swing.JLabel();
        jComboBoxNivelDificuldade = new javax.swing.JComboBox();
        jLabelGabarito = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaGabaritoDiscursiva = new javax.swing.JTextArea();
        jRadioButtonGabaritoMEa = new javax.swing.JRadioButton();
        jRadioButtonGabaritoMEb = new javax.swing.JRadioButton();
        jRadioButtonGabaritoMEc = new javax.swing.JRadioButton();
        jRadioButtonGabaritoMEd = new javax.swing.JRadioButton();
        jRadioButtonGabaritoMEe = new javax.swing.JRadioButton();
        jButtonConfirma = new javax.swing.JButton();
        jButtonLimpar = new javax.swing.JButton();
        jLabelEnunciado = new javax.swing.JLabel();
        jScrollPane8 = new javax.swing.JScrollPane();
        jTextAreaEnunciado = new javax.swing.JTextArea();
        jTextFieldOpcaoMEa = new javax.swing.JTextField();
        jTextFieldOpcaoMEb = new javax.swing.JTextField();
        jTextFieldOpcaoMEc = new javax.swing.JTextField();
        jTextFieldOpcaoMEd = new javax.swing.JTextField();
        jTextFieldOpcaoMEe = new javax.swing.JTextField();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelDisciplina.setText("Escolha a disciplina:");

        jComboBoxListaDeDisciplinas1.setModel(jComboBoxListaDeDisciplinas1.getModel());
        jComboBoxListaDeDisciplinas1.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jComboBoxListaDeDisciplinas1ComponentShown(evt);
            }
        });
        jComboBoxListaDeDisciplinas1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxListaDeDisciplinas1ItemStateChanged(evt);
            }
        });
        jComboBoxListaDeDisciplinas1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxListaDeDisciplinas1ActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jListTopicos);

        jLabelTopicos.setText("Escolha o(s) tópico(s):");

        jLabelTitulo.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabelTitulo.setText("Escolha as opções para edição de questão");

        jLabelTipoQuestao.setText("Escolha o tipo de questão:");

        buttonGroupTipoQuestao.add(jRadioButtonDiscursiva);
        jRadioButtonDiscursiva.setText("Discursiva");
        jRadioButtonDiscursiva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonDiscursivaActionPerformed(evt);
            }
        });

        buttonGroupTipoQuestao.add(jRadioButtonMultEscolha);
        jRadioButtonMultEscolha.setText("Múltipla escolha");
        jRadioButtonMultEscolha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMultEscolhaActionPerformed(evt);
            }
        });

        jLabelNivelDificuldade.setText("Escolha o nível de dificuldade:");

        jComboBoxNivelDificuldade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Fácil", "Médio", "Difícil" }));

        jLabelGabarito.setText("Preencha o gabarito:");

        jTextAreaGabaritoDiscursiva.setColumns(20);
        jTextAreaGabaritoDiscursiva.setRows(5);
        jScrollPane2.setViewportView(jTextAreaGabaritoDiscursiva);

        buttonGroupGabaritoME.add(jRadioButtonGabaritoMEa);
        jRadioButtonGabaritoMEa.setText("a");

        buttonGroupGabaritoME.add(jRadioButtonGabaritoMEb);
        jRadioButtonGabaritoMEb.setText("b");

        buttonGroupGabaritoME.add(jRadioButtonGabaritoMEc);
        jRadioButtonGabaritoMEc.setText("c");

        buttonGroupGabaritoME.add(jRadioButtonGabaritoMEd);
        jRadioButtonGabaritoMEd.setText("d");

        buttonGroupGabaritoME.add(jRadioButtonGabaritoMEe);
        jRadioButtonGabaritoMEe.setText("e");

        jButtonConfirma.setText("Confirma");
        jButtonConfirma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmaActionPerformed(evt);
            }
        });

        jButtonLimpar.setText("Limpar");
        jButtonLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimparActionPerformed(evt);
            }
        });

        jLabelEnunciado.setText("Preencha o enunciado:");

        jTextAreaEnunciado.setColumns(20);
        jTextAreaEnunciado.setRows(5);
        jScrollPane8.setViewportView(jTextAreaEnunciado);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 623, Short.MAX_VALUE)
                    .addComponent(jLabelTitulo)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelTipoQuestao)
                            .addComponent(jLabelDisciplina)
                            .addComponent(jLabelTopicos)
                            .addComponent(jLabelNivelDificuldade))
                        .addGap(88, 88, 88)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxNivelDificuldade, 0, 392, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButtonDiscursiva)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 218, Short.MAX_VALUE)
                                .addComponent(jRadioButtonMultEscolha))
                            .addComponent(jComboBoxListaDeDisciplinas1, 0, 392, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRadioButtonGabaritoMEd)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldOpcaoMEd, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRadioButtonGabaritoMEe)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldOpcaoMEe, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButtonLimpar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 485, Short.MAX_VALUE)
                        .addComponent(jButtonConfirma))
                    .addComponent(jLabelEnunciado)
                    .addComponent(jLabelGabarito)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 623, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButtonGabaritoMEa)
                            .addComponent(jRadioButtonGabaritoMEb)
                            .addComponent(jRadioButtonGabaritoMEc))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldOpcaoMEc, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
                            .addComponent(jTextFieldOpcaoMEb, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
                            .addComponent(jTextFieldOpcaoMEa, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabelTitulo)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDisciplina)
                    .addComponent(jComboBoxListaDeDisciplinas1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelTopicos)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jLabelTipoQuestao)
                        .addGap(24, 24, 24)
                        .addComponent(jLabelNivelDificuldade))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jRadioButtonDiscursiva)
                            .addComponent(jRadioButtonMultEscolha))
                        .addGap(18, 18, 18)
                        .addComponent(jComboBoxNivelDificuldade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jLabelEnunciado)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabelGabarito)
                .addGap(26, 26, 26)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldOpcaoMEa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButtonGabaritoMEa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldOpcaoMEb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButtonGabaritoMEb))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldOpcaoMEc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButtonGabaritoMEc))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldOpcaoMEd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButtonGabaritoMEd))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldOpcaoMEe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButtonGabaritoMEe))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConfirma)
                    .addComponent(jButtonLimpar))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxListaDeDisciplinas1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxListaDeDisciplinas1ActionPerformed
      
}//GEN-LAST:event_jComboBoxListaDeDisciplinas1ActionPerformed

    private void ExibirDisciplina(){
        List<Disciplina> listaDisciplinas = new ArrayList();

        listaDisciplinas = FuncoesDisciplinaGUI.ListarDisciplinas();
        String nomeDisciplina;

        for(int i = 0; i < listaDisciplinas.size(); i++){
            nomeDisciplina = listaDisciplinas.get(i).getNomeDisciplina();
            jComboBoxListaDeDisciplinas1.addItem(nomeDisciplina);
        }
        CarregaTopicos();
    }

    private void CarregaTopicos(){
        List<Disciplina> listaDisciplinas = new ArrayList();
        List<String> topicos = new ArrayList();
        DefaultListModel listModel = new DefaultListModel();
        String disciplina = (String) jComboBoxListaDeDisciplinas1.getSelectedItem();
        String listaTopicos;
        String nomeDisciplina;
        String topico;
       
        try {
            listaDisciplinas = DisciplinaBO.getInstance().listar();
        } catch(Exception ex) {
            Logger.getLogger(QuestaoEdicaoGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        for(int i = 0; i < listaDisciplinas.size(); i++){
            nomeDisciplina = listaDisciplinas.get(i).getNomeDisciplina();
            if(nomeDisciplina.equalsIgnoreCase(disciplina)){
                listaTopicos = listaDisciplinas.get(i).getTopico();
                topicos = FuncoesQuestoesGUI.listarTopicos(listaTopicos);
                for(int j = 0 ;j < topicos.size();j++){
                    topico = topicos.get(j);
                    listModel.addElement(topico);
                }
                jListTopicos.setModel(listModel);
            }
        }
    }

    private void CarregaTopicoDisciplina (String disciplina){
        List<Disciplina> listaDisciplinas = new ArrayList();
        List<String> topicos = new ArrayList();
        DefaultListModel listModel = new DefaultListModel();
        String listaTopicos;
        String nomeDisciplina;
        String topico;
        
        try {
            listaDisciplinas = DisciplinaBO.getInstance().listar();
        } catch(Exception ex) {
            Logger.getLogger(QuestaoEdicaoGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        for(int i = 0; i < listaDisciplinas.size(); i++){
            nomeDisciplina = listaDisciplinas.get(i).getNomeDisciplina();
            if(nomeDisciplina.equalsIgnoreCase(disciplina)){
                listaTopicos = listaDisciplinas.get(i).getTopico();
                topicos = FuncoesQuestoesGUI.listarTopicos(listaTopicos);
                for(int j = 0 ;j < topicos.size();j++){
                    topico = topicos.get(j);
                    listModel.addElement(topico);
                }
                jListTopicos.setModel(listModel);
            }
        }
    }

    private void jButtonLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimparActionPerformed
        jTextAreaEnunciado.setText(null);
        jTextAreaGabaritoDiscursiva.setText(null);
        jTextFieldOpcaoMEa.setText(null);
        jTextFieldOpcaoMEb.setText(null);
        jTextFieldOpcaoMEc.setText(null);
        jTextFieldOpcaoMEd.setText(null);
        jTextFieldOpcaoMEe.setText(null);
    }//GEN-LAST:event_jButtonLimparActionPerformed

    private String TopicosRelacionados(){
        String topicosSelec;
        String topicos = null;
                                                
        Object[] topicosSelecionados = jListTopicos.getSelectedValues();
               
        for(int i = 0; i < topicosSelecionados.length; i++){
            topicosSelec = (String)topicosSelecionados[i];
            //topicosSelec.add(topTeste);
            topicos = topicos + ";" + topicosSelec;                   
        }
        return topicos;
    }

    private void jButtonConfirmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmaActionPerformed
        try{
            String disciplinaSelecionada = null;
            List<Disciplina> listaDisciplinas = new ArrayList();
            listaDisciplinas = FuncoesDisciplinaGUI.ListarDisciplinas();
            int i = 0;

            if(alteracao == 0){
                disciplinaSelecionada = (String) jComboBoxListaDeDisciplinas1.getSelectedItem();
                i = FuncoesDisciplinaGUI.ProcurarDisciplina(disciplinaSelecionada);
            }
            
            Questao questao = new Questao();
            questao.setTopico(TopicosRelacionados()); 
            String dificuldade = (String) jComboBoxNivelDificuldade.getSelectedItem();
            if(dificuldade.equalsIgnoreCase("Fácil"))
                questao.setDificuldade(GrauDificuldade.FACIL);
            else if (dificuldade.equalsIgnoreCase("Médio"))
                questao.setDificuldade(GrauDificuldade.MEDIA);
            else
                questao.setDificuldade(GrauDificuldade.DIFICIL);
               
            questao.setEnunciadoQuestao(jTextAreaEnunciado.getText());
            if (jTextAreaEnunciado.getText().isEmpty() == true){
                 JOptionPane.showMessageDialog(this,"Existem campos em branco!",this.getTitle() ,JOptionPane.INFORMATION_MESSAGE);
                 dispose();
            }

            if(jRadioButtonDiscursiva.isSelected()){
                questao.setResposta(jTextAreaGabaritoDiscursiva.getText());
                if (jTextAreaGabaritoDiscursiva.getText().isEmpty() == true){
                 JOptionPane.showMessageDialog(this,"Existem campos em branco!",this.getTitle() ,JOptionPane.INFORMATION_MESSAGE);
                 dispose();
                }
                
                questao.setTipoQuestao(TipoQuestao.DISCURSIVA);
                if(alteracao == 0){
                    try {
                        DisciplinaBO.getInstance().listar().get(i).incluirQuestao(questao);
                    } catch(Exception ex) {
                        Logger.getLogger(QuestaoEdicaoGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else{
                    try {
                        DisciplinaBO.getInstance().listar().get(FuncoesDisciplinaGUI.ProcurarDisciplina(nomeDisciplinaAlterar)).alterarQuestao(id, questao);
                    } catch (Exception ex) {
                        Logger.getLogger(QuestaoEdicaoGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            else if(jRadioButtonMultEscolha.isSelected()){
                List<String> listaOpcoes = new ArrayList<String>();
                listaOpcoes.add(jTextFieldOpcaoMEa.getText());
                listaOpcoes.add(jTextFieldOpcaoMEb.getText());
                listaOpcoes.add(jTextFieldOpcaoMEc.getText());
                listaOpcoes.add(jTextFieldOpcaoMEd.getText());
                listaOpcoes.add(jTextFieldOpcaoMEe.getText());
                questao.setOpcoes(listaOpcoes);
                questao.setTipoQuestao(TipoQuestao.MULTIPLA_ESCOLHA);
                              
                if(jRadioButtonGabaritoMEa.isSelected())
                    questao.setOpcaoCorreta(0);
                else if(jRadioButtonGabaritoMEb.isSelected())
                    questao.setOpcaoCorreta(1);
                else if(jRadioButtonGabaritoMEc.isSelected())
                    questao.setOpcaoCorreta(2);
                else if(jRadioButtonGabaritoMEd.isSelected())
                    questao.setOpcaoCorreta(3);
                else if(jRadioButtonGabaritoMEe.isSelected())
                    questao.setOpcaoCorreta(4);
                else{
                 JOptionPane.showMessageDialog(this,"Existem campos em branco!",this.getTitle() ,JOptionPane.INFORMATION_MESSAGE);
                 dispose();
                }
                if(alteracao == 0){
                    try {
                        DisciplinaBO.getInstance().listar().get(i).incluirQuestao(questao);
                    } catch (Exception ex) {
                        Logger.getLogger(QuestaoEdicaoGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }       
                else{
                    try {
                        DisciplinaBO.getInstance().listar().get(FuncoesDisciplinaGUI.ProcurarDisciplina(nomeDisciplinaAlterar)).alterarQuestao(id, questao);
                    } catch (Exception ex) {
                        Logger.getLogger(QuestaoEdicaoGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }      
            }   
            else
                JOptionPane.showMessageDialog(this, "Todos as opções devem ser preenchidas", this.getTitle(), JOptionPane.ERROR_MESSAGE);
            JOptionPane.showMessageDialog(this,"Operação realizada com sucesso!",this.getTitle() ,JOptionPane.INFORMATION_MESSAGE);
            QuestaoEdicaoGUI.this.dispose();
        } catch(NumberFormatException e) {
            showErrorMessage(e.getMessage());
        } 
    }

    private void showErrorMessage(String message) {
        JOptionPane.showMessageDialog(this,"Entrada inválida " + message,this.getTitle() ,JOptionPane.ERROR_MESSAGE);
    }//GEN-LAST:event_jButtonConfirmaActionPerformed

    private void jComboBoxListaDeDisciplinas1ComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jComboBoxListaDeDisciplinas1ComponentShown
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxListaDeDisciplinas1ComponentShown

    private void jRadioButtonDiscursivaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonDiscursivaActionPerformed
        jTextAreaGabaritoDiscursiva.setVisible(true);
        jTextFieldOpcaoMEa.setVisible(false);
        jTextFieldOpcaoMEb.setVisible(false);
        jTextFieldOpcaoMEc.setVisible(false);
        jTextFieldOpcaoMEd.setVisible(false);
        jTextFieldOpcaoMEe.setVisible(false);
        jRadioButtonGabaritoMEa.setVisible(false);
        jRadioButtonGabaritoMEb.setVisible(false);
        jRadioButtonGabaritoMEc.setVisible(false);
        jRadioButtonGabaritoMEd.setVisible(false);
        jRadioButtonGabaritoMEe.setVisible(false);
    }//GEN-LAST:event_jRadioButtonDiscursivaActionPerformed

    private void jRadioButtonMultEscolhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMultEscolhaActionPerformed
        jTextAreaGabaritoDiscursiva.setVisible(false);
        jTextFieldOpcaoMEa.setVisible(true);
        jTextFieldOpcaoMEb.setVisible(true);
        jTextFieldOpcaoMEc.setVisible(true);
        jTextFieldOpcaoMEd.setVisible(true);
        jTextFieldOpcaoMEe.setVisible(true);
        jRadioButtonGabaritoMEa.setVisible(true);
        jRadioButtonGabaritoMEb.setVisible(true);
        jRadioButtonGabaritoMEc.setVisible(true);
        jRadioButtonGabaritoMEd.setVisible(true);
        jRadioButtonGabaritoMEe.setVisible(true);
    }//GEN-LAST:event_jRadioButtonMultEscolhaActionPerformed

    private void jComboBoxListaDeDisciplinas1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxListaDeDisciplinas1ItemStateChanged
        CarregaTopicos();
    }//GEN-LAST:event_jComboBoxListaDeDisciplinas1ItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new QuestaoEdicaoGUI(null,0,0).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupGabaritoME;
    private javax.swing.ButtonGroup buttonGroupTipoQuestao;
    private javax.swing.JButton jButtonConfirma;
    private javax.swing.JButton jButtonLimpar;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBoxListaDeDisciplinas1;
    private javax.swing.JComboBox jComboBoxNivelDificuldade;
    private javax.swing.JLabel jLabelDisciplina;
    private javax.swing.JLabel jLabelEnunciado;
    private javax.swing.JLabel jLabelGabarito;
    private javax.swing.JLabel jLabelNivelDificuldade;
    private javax.swing.JLabel jLabelTipoQuestao;
    private javax.swing.JLabel jLabelTitulo;
    private javax.swing.JLabel jLabelTopicos;
    private javax.swing.JList jListTopicos;
    private javax.swing.JRadioButton jRadioButtonDiscursiva;
    private javax.swing.JRadioButton jRadioButtonGabaritoMEa;
    private javax.swing.JRadioButton jRadioButtonGabaritoMEb;
    private javax.swing.JRadioButton jRadioButtonGabaritoMEc;
    private javax.swing.JRadioButton jRadioButtonGabaritoMEd;
    private javax.swing.JRadioButton jRadioButtonGabaritoMEe;
    private javax.swing.JRadioButton jRadioButtonMultEscolha;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JTextArea jTextAreaEnunciado;
    private javax.swing.JTextArea jTextAreaGabaritoDiscursiva;
    private javax.swing.JTextField jTextFieldOpcaoMEa;
    private javax.swing.JTextField jTextFieldOpcaoMEb;
    private javax.swing.JTextField jTextFieldOpcaoMEc;
    private javax.swing.JTextField jTextFieldOpcaoMEd;
    private javax.swing.JTextField jTextFieldOpcaoMEe;
    // End of variables declaration//GEN-END:variables
}
